package textapp.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import textapp.dao.model.Group;
import textapp.dao.model.GroupUser;
import textapp.dao.model.User;

public class GroupDao {
	
	public Group createGroupWithMember(int ownerId, String name, int type, List<Integer> idList) {
		UserDao uDao = new UserDao();
		User owner = uDao.findById(ownerId);
		if (owner == null) {
			return null;
		}
		
		List<User> users = uDao.findUsersByIds(idList);
		if (users.size() <= 1) {
			return null;
		}
		
		EntityManager em = EMUtil.createEntityManager();
		em.getTransaction().begin();
		Group group = new Group();
		group.setName(name);
		group.setOwner(owner);
		group.setType(type);
		em.persist(group);
		em.getTransaction().commit();
		
		List<GroupUser> guList = new ArrayList<GroupUser>();
		em.getTransaction().begin();
		for (User u : users) {
			GroupUser gu = new GroupUser();
			System.out.println(group);
			System.out.println(u);
			gu.setGroup(group);
			gu.setUser(u);
			em.persist(gu);
			guList.add(gu);
		}
		em.getTransaction().commit();
		
		em.close();
		
		group.setGroupUsers(guList);
		return group;
	}
	
	public Group getGroupById(int groupId, boolean withUsers) {
		EntityManager em = EMUtil.createEntityManager();
		Group group = em.find(Group.class, groupId);
		if (group == null) {
			em.close();
			return null;
		}
		if (withUsers) {
			Query q = em.createNamedQuery("GroupUser.getByGroup");
			q.setParameter("group", group);
			List<GroupUser> guList = q.getResultList();
			group.setGroupUsers(guList);
		}
		em.close();
		
		return group;
	}
	
	public Group addUsersToGroup(int groupId, List<Integer> idList) {
		Group group = getGroupById(groupId, true);
		if (group == null) {
			return null;
		}
		
		UserDao uDao = new UserDao();
		
		List<User> users = uDao.findUsersByIds(idList);
		List<GroupUser> existings = group.getGroupUsers();
		
		EntityManager em = EMUtil.createEntityManager();
		em.getTransaction().begin();
		for (User u : users) {
			boolean found = false;
			for (GroupUser gu: existings) {
				if (gu.getUser().equals(u)) {
					found = true;
					break;
				}
			}
			if (!found) {
				GroupUser newGu = new GroupUser();
				newGu.setUser(u);
				newGu.setGroup(group);
				em.persist(newGu);
				group.getGroupUsers().add(newGu);
			}
		}
		em.getTransaction().commit();
		em.close();
		
		return group;
	}
	
	public boolean deleteGroup(int groupId) {
		boolean result = false;
		EntityManager em = EMUtil.createEntityManager();
		em.getTransaction().begin();
		Group group = getGroupById(groupId, true);
		try {
			List<GroupUser> existings = group.getGroupUsers();
			for (GroupUser gu: existings) {
				
				
				em.remove(em.find(GroupUser.class, gu.getId()));
			}
			em.remove(em.find(Group.class, group.getId()));
			result = true;
		} catch(Throwable t) {
			t.printStackTrace();
		}
		em.getTransaction().commit();
		em.close();
		return result;
	}
	

}
